var TreeMenu=new function()
{   
    this._url=null;

    document.write('<style type="text/css">');
    document.write('.treeCheckBox{height:11px; width:11px;vertical-align:middle}'); 
    document.write('.treeImg{cursor:pointer;vertical-align:text-bottom;margin-right:1px}');
    document.write('</style>');

	this.icon=new Array();
	this.icon["member"]='/img/child.gif'; 
	this.icon["open"]='/img/tree/home2.gif'; 
	this.icon["close"]='/img/tree/home1.gif';
	
	this.getChildren=function(_parentId)
	{
	  
		if (this.alreadyGetChileren(_parentId))
		{

		    var childContainer=document.getElementById(_parentId+"_subContainer");
		    if (childContainer)
		    {
		        childContainer.style.display=(childContainer.style.display=="none")?"":"none";
		        var _parentNode=document.getElementById(_parentId);
		        if (_parentNode.firstChild && _parentNode.firstChild.tagName=="IMG")
		        {
		            _parentNode.firstChild.src=(childContainer.style.display=="none")?this.icon["close"]:this.icon["open"];
		        }
		    }
		    return;
		}
		
		var processRequest=function(obj)
		{
		    TreeMenu.addChildren(_parentId,obj.responseXML);         
		}
		Request.send(this._url+"?pId="+_parentId,"",processRequest);
	}	

	this.addChildren=function(_parentId,_data)
	{   
	    if (this.alreadyGetChileren(_parentId))
	    {	     
	        return;
	    }
	    var _parentNode=document.getElementById(_parentId);
		
	    if (_parentNode.firstChild && _parentNode.firstChild.tagName=="IMG")
	    {
	        _parentNode.firstChild.src=this.icon["open"];		 
	    }	    	       
	    _nodeContainer=document.createElement("div");
		_nodeContainer.id=_parentId+"_subContainer";
		_parentNode.appendChild(_nodeContainer);
		var _children=_data.getElementsByTagName("root")[0].childNodes;
		var _child=null;
		var _point=this;
		for(var i=0; i<_children.length; i++)
		{
			_child=_children[i];				
			_node=document.createElement("div");
			if (i!=_children.length-1)
			{
			    _node.style.cssText="padding-bottom:5px";
			}			
			_node.innerHTML="";
			_node.id=_child.getAttribute("id");	
			if (_child.getAttribute("hasChildren")=="2")
			{
			    _node.innerHTML+='<img class="treeImg" onclick="TreeMenu.getChildren('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("dorgacode")+"\'"+')" src="'+this.icon["close"]+'"/>';
			    _node.innerHTML+='<span style="line-height:16px;height:16px;a:link{text-decoration: none}" name="treeText" onclick="treeNodeChoosed(this);"> '+_child.firstChild.data+'</span>';			    
			}			
			else if (_child.getAttribute("hasChildren")=="1")
			{
			    _node.innerHTML+='<img class="treeImg" onclick="TreeMenu.getChildren('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("dorgacode")+"\'"+')" src="'+this.icon["close"]+'"/>';
				//if (_child.getAttribute("flag")=='1'){
    			//	_node.innerHTML+='<span style="line-height:16px;height:16px;a:link{text-decoration: none}" name="treeText" onclick="treeNodeChoosed(this);">'+_child.firstChild.data+'</span>';			    
				//}else{
				//    _node.innerHTML+='<span style="cursor:pointer;line-height:16px;height:16px;a:link{text-decoration: none}" name="treeText" onclick="treeNodeChoosed(this);"> <a href="#"  onclick="tj('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("dorgacode")+"\'"+')">'+_child.firstChild.data+'</a>'+'</span>';			    
				//}
				 _node.innerHTML+='<span style="cursor:pointer;line-height:16px;height:16px;a:link{text-decoration: none}" name="treeText" onclick="treeNodeChoosed(this);"> <a href="#"  onclick="tj('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("flag")+"\'"+','+"\'"+_child.getAttribute("orgatypeid")+"\'"+')">'+_child.firstChild.data+'</a>'+'</span>';			    
			    
			}
			else if (_child.getAttribute("hasChildren")==0)
			{
			    _node.innerHTML+='<img class="treeImg" onclick="try{treeNodeChoosed(this.nextSibling);}catch(e){alert(e.message);}" src="'+this.icon["member"]+'" style="margin-left:0px"/>';
				//if (_child.getAttribute("flag")=='1'){
                //   _node.innerHTML+='<span style="line-height:16px;height:16px;a:link{text-decoration: none};a:visited{text-decoration: none};a:hover {text-decoration: none} " name="treeText" onclick="treeNodeChoosed(this);"> '+_child.firstChild.data+'</span>';			    
				//}else{
				//    _node.innerHTML+='<span style="cursor:pointer;line-height:16px;height:16px;a:link{text-decoration: none};a:visited{text-decoration: none};a:hover {text-decoration: none} " name="treeText" onclick="treeNodeChoosed(this);"> <a href="#"  onclick="tj('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("dorgacode")+"\'"+')">'+_child.firstChild.data+'</a>'+'</span>';			    
				//}
			    //_node.innerHTML+='<span style="cursor:pointer;line-height:16px;height:16px;a:link{text-decoration: none};a:visited{text-decoration: none};a:hover {text-decoration: none} " name="treeText" onclick="treeNodeChoosed(this);"> <a href="#"  onclick="tj('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("dorgacode")+"\'"+')">'+_child.firstChild.data+'</a>'+'</span>';			    
                  _node.innerHTML+='<span style="cursor:pointer;line-height:16px;height:16px;a:link{text-decoration: none};a:visited{text-decoration: none};a:hover {text-decoration: none} " name="treeText" onclick="treeNodeChoosed(this);"> <a href="#"  onclick="tj('+"\'"+_child.getAttribute("id")+"\'"+','+"\'"+_child.getAttribute("flag")+"\'"+','+"\'"+_child.getAttribute("orgatypeid")+"\'"+')">'+_child.firstChild.data+'</a>'+'</span>';			    
			}
			_nodeContainer.appendChild(_node);					
		}
		_nodeContainer.style.cssText="border-left:0px solid #ccc;margin-left:0px;margin-top:5px;padding-left:10px";	    
	}
	
	this.alreadyGetChileren=function(_nodeId)
	{ 
	    var obj=document.getElementById(_nodeId+"_subContainer");
	    if (obj)
	    {	       
	        return true;               
	    }
	    return false;	    
	}	
}

function treeNodeChoosed(_obj)
{
    var choosedColor="lightblue";
    var unChoosedColor="white";

    if (_obj.style.backgroundColor==choosedColor)
    {
        _obj.style.backgroundColor=unChoosedColor;           
    }
    else
    {
        //var allNodeText=document.getElementsByName("treeText");
        var allNodeText=document.getElementsByTagName("SPAN");
        for (var i=0; i<allNodeText.length; i++)
        {
            allNodeText[i].style.backgroundColor=unChoosedColor;
        }
        _obj.style.backgroundColor=choosedColor;
    }    
}